package com.hougong.im.utils;

import com.hougong.im.body.BodyToBytes;
import com.hougong.im.body.request.GenericRequest;
import com.hougong.im.common.GlobalCode;
import com.hougong.im.transport.BaseFrame;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 发送消息到通道channel中
 */
public class SendUtils {

    private static final Logger LOGGER= LoggerFactory.getLogger(SendUtils.class);
    private SendUtils(){}
    public static void sendResponse(Channel channel,short bizType, BodyToBytes response){
        BaseFrame baseFrame=new BaseFrame();
        baseFrame.setMessageType((byte) 0x02);
        baseFrame.setBizType(bizType);
        byte[] body=response.toBytes();
        baseFrame.setBody(body);
        ChannelFuture channelFuture = channel.writeAndFlush(baseFrame);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
                    LOGGER.info("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送成功");
                }else {
                    channel.writeAndFlush(baseFrame)
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    if(future.isSuccess()){
                                        LOGGER.info("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送成功");
                                    }else {
                                        LOGGER.error("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送失败");
                                    }
                                }
                            });
                }
            }
        });
    }
    public static void sendNotice(Channel channel,short bizType, BodyToBytes response){
        BaseFrame baseFrame=new BaseFrame();
        baseFrame.setMessageType((byte) 0x03);
        baseFrame.setBizType(bizType);
        byte[] body=response.toBytes();
        baseFrame.setBody(body);
        ChannelFuture channelFuture = channel.writeAndFlush(baseFrame);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
                    LOGGER.info("业务类型:0x"+bizType+" body:"+StringUtils.bytesToHexString(body)+ "Notice消息发送成功");
                }else {
                    channel.writeAndFlush(baseFrame)
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    if(future.isSuccess()){
                                        LOGGER.info("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Notice消息发送成功");
                                    }else {
                                        LOGGER.error("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Notice消息发送失败");
                                    }
                                }
                            });
                }
            }
        });
    }

    /**
     * 发送通用应答
     * @param channel
     * @param genericRequest
     */
    public static void sendGenericResponse(Channel channel, GenericRequest genericRequest){
        sendResponse(channel, GlobalCode.Server_Generic_Response.getBizCode(),genericRequest);
    }

    /**
     * 发送通用应答
     * @param channel
     * @param request  得到的相应
     * @param result   操作结果 0成功/确认  1失败   2消息有误
     */
    public static void sendGenericResponse(Channel channel,BaseFrame request,byte result){
        GenericRequest genericRequest=new GenericRequest();
        genericRequest.setReBizType(request.getBizType());
        genericRequest.setReStreamId(request.getStreamId());
        genericRequest.setResult(result);
        sendGenericResponse(channel,genericRequest);
    }
}
